package com.example.myapplication.module.adapter

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module1.ItemGridSpanSizeLookup
import com.example.myapplication.module1.adapter.CategoryContentAdapter
import com.example.mylibrary.adapter.BaseRecyclerAdapter

class CategoryListAdapter(context: Context) : BaseRecyclerAdapter(context) {
    var tabList: Array<String> = arrayOf()

    var customerMangerList = mutableListOf<Any>()
    var pswInfoList = mutableListOf<Any>()
    var cashBankList = mutableListOf<Any>()
    var statisticsList = mutableListOf<Any>()

    fun setData(
        tabList: Array<String>,
        customerMangerList: MutableList<Any>,
        pswInfoList: MutableList<Any>,
        cashBankList: MutableList<Any>,
        statisticsList: MutableList<Any>
    ) {
        this.tabList = tabList
        this.customerMangerList = customerMangerList
        this.pswInfoList = pswInfoList
        this.cashBankList = cashBankList
        this.statisticsList = statisticsList
        notifyDataSetChanged()
    }

    lateinit var gridLayoutManager: GridLayoutManager
    override fun getViewHolder(baseViewHolder: BaseViewHolder) {

    }

    override fun getResId(viewType: Int): Int {
        gridLayoutManager = GridLayoutManager(context, 4, RecyclerView.VERTICAL, false)
        return R.layout.category_list
    }

    override fun bindHolder(holder: BaseViewHolder, position: Int) {
        val rvCategory = holder.getView<RecyclerView>(R.id.rvCategory)
        val categoryContentAdapter = CategoryContentAdapter(context)
        categoryContentAdapter.clickListener = object : ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val mutableList = categoryContentAdapter.mutableList
                when (mutableList[position]) {
                    is ItemBean -> {
                        val itemBean = mutableList[position] as ItemBean
                        categoryClickListener?.onClickListenerCategory(itemBean = itemBean)
                    }
                }
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
//                TODO("Not yet implemented")
                return true
            }

        }
        rvCategory.adapter = categoryContentAdapter
        gridLayoutManager = rvCategory.layoutManager as GridLayoutManager
        when (position) {
            0 -> {
                gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(customerMangerList, 4)
                categoryContentAdapter.mutableList = customerMangerList
            }
            1 -> {
                gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(pswInfoList, 4)
                categoryContentAdapter.mutableList = pswInfoList
            }
            2 -> {
                gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(cashBankList, 4)
                categoryContentAdapter.mutableList = cashBankList
            }
            3 -> {
                gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(statisticsList, 4)
                categoryContentAdapter.mutableList = statisticsList
            }
        }
    }

    override fun getItemCount(): Int {
        return tabList.size
    }

    interface CategoryClickListener {
        fun onClickListenerCategory(itemBean: ItemBean)
    }

    var categoryClickListener: CategoryClickListener? = null
}